#
# Converts <http://www.loc.gov/standards/iso639-2/ISO-639-2_8859-1.txt>
# into Java code that returns the OASIS Open PSIs for
# ISO 639 language codes: <http://psi.oasis-open.org/iso/639/>
#
# The OASIS PSIs use the alpha-3 terminological code iff it differs
# from the alpha-3 bibliographic code.
#
import codecs
from urllib2 import urlopen

def char_array(s):
    return '"%s"' % s

def build_alpha2_mapping(d):
    l = ['switch (ch[0]) {']
    keys = sorted(d.keys())
    d2 = {}
    for k in keys:
        first, second = k
        if first in d2:
            d2[first].append(second)
        else:
            d2[first] = [second]
    for f in sorted(d2.keys()):
        l.append("      case '%s':" % f)
        if len(d2[f]) == 1:
            s = d2[f][0]
            result = char_array(d['%s%s' % (f,s)])
            l.append('''              return ch[1] == '%s' ? %s : null;''' % (s, result))
        else:
            l.append('          switch (ch[1]) {')
            for s in d2[f]:
                result = char_array(d['%s%s' % (f,s)])
                l.append('''              case '%s': return %s;''' % (s, result))
            l.append('          }')
            l.append('          return null;')
    l.append('}')
    l.append('return null;')
    return l


def build_alpha3_mapping(codes, d):
    l = ['switch (ch[0]) {']
    keys = sorted(codes)
    d2 = {}
    for k in keys:
        first, second, third = k
        if first in d2:
            if second in d2[first]:
                d2[first][second].append(third)
            else:
                d2[first][second] = [third]
        else:
            d2[first] = {}
            d2[first][second] = [third]
    for f in sorted(d2.keys()):
        l.append("      case '%s':" % f)
        l.append('          switch (ch[1]) {')
        for s in sorted(d2[f].keys()):
            l.append("              case '%s':" % s)
            if len(d2[f][s]) == 1:
                t = d2[f][s][0]
                term = d.get('%s%s%s' % (f, s, t))
                if term:
                    result = '"%s"' % term
                else:
                    result = 'lang'
                l.append("                      return ch[2] == '%s' ? %s : null;" % (t, result)) 
            else:
                l.append('                  switch (ch[2]) {')
                for t in sorted(d2[f][s]):
                    term = d.get('%s%s%s' % (f, s, t))
                    if term:
                        result = '"%s"' % term
                    else:
                        result = 'lang'
                    l.append("                      case '%s': return %s;" % (t, result)) 
                l.append('                  }')
                l.append('                  return null;')
        l.append('          }')
        l.append('          return null;')
    l.append('}')
    l.append('return null;')
    return l
            
    

f = urlopen('http://www.loc.gov/standards/iso639-2/ISO-639-2_8859-1.txt')

alpha2toterm = {}
bibtoterm = {}
alpha3codes = []

for i, l in enumerate(f):
    if i == 0:
        continue # First line contains rubbish
    content = l.split('|')
    bib, term, alpha2 = content[0], content[1], content[2]
    if bib == 'qaa-qtz': # skip "Reserved for local use"
        continue
    alpha3codes.append(bib)
    if term:
        alpha3codes.append(term)
        bibtoterm[bib] = term
    if alpha2:
        alpha2toterm[alpha2] = term or bib

alpha2mapping = build_alpha2_mapping(alpha2toterm)
alpha3mapping = build_alpha3_mapping(alpha3codes, bibtoterm)

   

print '\n'.join(alpha2mapping)

print '================'

print '\n'.join(alpha3mapping)
